package com.woniu.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.model.Result;
import com.woniu.product.form.AddCardForm;
import com.woniu.product.form.UpdateCardForm;
import com.woniu.product.from.memberCard.LoadMemberCardForm;
import com.woniu.product.model.MemberCard;
import com.woniu.product.param.AddCardParam;
import com.woniu.product.param.UpdateCardParam;
import com.woniu.product.param.memberCard.LoadMemberCardParam;
import com.woniu.product.service.MemberCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@RestController
@RequestMapping("/memberCard")
@Api(tags = "会员卡相关接口")
public class MemberCardController {
    @Resource
    private MemberCardService memberCardService;

    @PostMapping("/addCard")
    @ApiOperation("创建新会员卡")
    public Result addCard(@RequestBody AddCardForm form)throws Exception{
        AddCardParam param = BeanUtil.toBean(form, AddCardParam.class);
        memberCardService.addCard(param);
        return Result.getResult(null).setMsg("新增成功").setCode(200);
    }

    /**
     * 通过会员卡ID查询会员卡
     * @param cardId
     * @return
     */
    @GetMapping("/getCardById")
    @ApiOperation("通过会员卡ID查询会员卡")
    public Result<MemberCard> getCardById(@RequestParam(value="cardId") String cardId)throws Exception {
        MemberCard memberCard = memberCardService.getCardById(cardId);
        return Result.getResult(MemberCard.class).setData(memberCard).setMsg("查询成功").setCode(200);
    }

    @PostMapping("/update")
    @ApiOperation("更新会员卡信息")
    public Result<?> update(@RequestBody UpdateCardForm form)throws Exception {
        UpdateCardParam param = BeanUtil.toBean(form, UpdateCardParam.class);
        memberCardService.update(param);
        return Result.getResult(null).setMsg("更新成功").setCode(200);
    }

    @GetMapping("/list")
    @ApiOperation("查询所有会员卡")
    public Result<?> list() {
        List<MemberCard> list = memberCardService.list();
        return Result.getListResult(MemberCard.class).setData(list).setMsg("查询成功").setCode(200);
    }

    @PostMapping("/load")
    @ApiOperation("查询场馆的会员卡")
    public Result load(@RequestBody LoadMemberCardForm form) {
        LoadMemberCardParam param = BeanUtil.toBean(form, LoadMemberCardParam.class);
        PageInfo<MemberCard> list = memberCardService.load(param);
        return Result.getResult().setData(list).setMsg("查询成功");
    }


    /**
     * 计算会员卡剩余有效期
     * @param cardId
     * @return
     */
    @GetMapping("/calculateRemainingDays")
    @ApiOperation("计算会员卡剩余有效期")
    public Result<?> calculateRemainingDays(@RequestParam String cardId) {
        long remainingDays = memberCardService.calculateRemainingDays(cardId);
        return Result.getResult(Long.class).setData(remainingDays).setMsg("计算成功").setCode(200);
    }
}

